package de.hska.bestellverwaltung.db;

import static javax.persistence.CascadeType.ALL;
import static de.hska.util.EjbConstants.KEINE_ID;
import static de.hska.util.EjbConstants.ERSTE_VERSION;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Version;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.hska.util.MessagesUtil;


@Entity
@Table(name="lieferung")
public class Lieferung implements java.io.Serializable {
	private static final long serialVersionUID = 7560752199018702446L;
	protected static final Log LOG = LogFactory.getLog(Lieferung.class);
	protected static final boolean DEBUG = LOG.isDebugEnabled();
	protected static final boolean TRACE = LOG.isTraceEnabled();

	@Id
	@GeneratedValue(generator="lieferung_sequence_name")
	@SequenceGenerator(name="lieferung_sequence_name", sequenceName="lieferung_l_id_seq", allocationSize=1)
	@Column(name="l_id", nullable=false)
	private Long id = KEINE_ID;

	@Version
	private int version = ERSTE_VERSION;

	@Column(name="liefernr", length=12, unique=true)
	private String lieferNr;

	@ManyToMany(mappedBy="lieferungen", cascade={ALL})
	@OrderBy("id ASC")
	private List<Bestellung> bestellungen;

	public Lieferung() {
		super();
	}
	
	@PostPersist
	protected void logDbId() {
		if (DEBUG) {
			final String msg = MessagesUtil.getMessage("log.bestellverwaltung.lieferung.create", id, this);
			LOG.debug(msg);
		}
	}

	@PostUpdate
	protected void logNeueVersion() {
		if (TRACE) {
			final String msg = MessagesUtil.getMessage("log.bestellverwaltung.lieferung.update", id, version);
			LOG.trace(msg);
		}
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	}

	public String getLieferNr() {
		return lieferNr;
	}
	public void setLieferNr(String lieferNr) {
		this.lieferNr = lieferNr;
	}

	public List<Bestellung> getBestellungen() {
		return bestellungen;
	}
	public void setBestellungen(List<Bestellung> bestellungen) {
		this.bestellungen = bestellungen;
	}

	@Override
	public String toString() {
		return "{id=" + id + ", version=" + version + ", lieferNr=" + lieferNr + '}';
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((lieferNr == null) ? 0 : lieferNr.hashCode());
		result = PRIME * result + version;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Lieferung other = (Lieferung) obj;
		if (lieferNr == null) {
			if (other.lieferNr != null)
				return false;
		} else if (!lieferNr.equals(other.lieferNr))
			return false;
		if (version != other.version)
			return false;
		return true;
	}


}